package com.local.videoplayer.ads;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.ads.Ad;
import com.facebook.ads.AdChoicesView;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.AdSettings;
import com.facebook.ads.MediaView;
import com.facebook.ads.NativeAd;
import com.jaaksi.superclean.R;

public class FacebookBigUtil {

    public static String Facebook_native_placement_id = "319564711838887_319892635139428";
    public static final long AD_TIME = 60 * 60 * 1000l;
    private static NativeAd preloadFacebookAd;
    private static View preloadFacebookAdView;

    public static void setFacebookNativePlacementId(String facebookNativePlacementId) {
        Facebook_native_placement_id = facebookNativePlacementId;
    }

    public static void preloadAd(Context context) {
        if (!needReloadAd(context))
            return;

        forceReloadAd(context);
    }

    public static void forceReloadAd(final Context context) {
        try {
            AdSettings.addTestDevice("274f5a3117e7d3e38d3191512062117d");
            AdSettings.addTestDevice("d58ded9f18626c915c93ce4bc84a9a5b");
            //samsung
            AdSettings.addTestDevice("1615A729608C0387DCD80617AEBF91A0");
            NativeAd ad = new NativeAd(context, Facebook_native_placement_id);
            ad.setAdListener(new AdListener() {
                @Override
                public void onError(Ad ad, AdError adError) {
                }

                @Override
                public void onAdLoaded(Ad ad) {
                    preloadFacebookAd = (NativeAd) ad;
                    preloadFacebookAdView = LayoutInflater.from(context).inflate(R.layout.facebook_big, null);
                    inflateFacebookAd(context, (NativeAd) ad, preloadFacebookAdView);
                    AdPreference.getInstance(context).setLastBigAdTime(System.currentTimeMillis());

                }

                @Override
                public void onAdClicked(Ad ad) {

                }

                @Override
                public void onLoggingImpression(Ad ad) {

                }
            });
            ad.loadAd(NativeAd.MediaCacheFlag.ALL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static boolean needReloadAd(Context context) {
        if (preloadFacebookAd == null)
            return true;

        return (System.currentTimeMillis() - AdPreference.getInstance(context).getLastBigAdTime()) > AD_TIME;
    }

    public static NativeAd getPreloadFacebookAd(Context context) {
        if (needReloadAd(context))
            return null;

        return preloadFacebookAd;
    }


    public static View getPreloadFacebookAdView() {
        View view = preloadFacebookAdView;
        preloadFacebookAdView = null;
        return view;
    }

    public static void inflateFacebookAd(Context context, NativeAd nativeAd, View adView) {
        if (nativeAd == null)
            return;
        // Create native UI using the ad metadata.

        ImageView nativeAdIcon = (ImageView) adView.findViewById(R.id.nativeAdIcon);
        TextView nativeAdTitle = (TextView) adView.findViewById(R.id.nativeAdTitle);
        Button nativeCallToAction = (Button) adView.findViewById(R.id.nativeAdCallToAction);
        MediaView nativeAdMedia = (MediaView) adView.findViewById(R.id.native_ad_media);
        ImageView nativeAdImage = (ImageView) adView.findViewById(R.id.nativeAdImage);
        ViewGroup adChoiceContainer = (ViewGroup) adView.findViewById(R.id.ad_choice_container);
        nativeAdMedia.setAutoplay(true);

        nativeAdTitle.setText(nativeAd.getAdTitle());
        nativeCallToAction.setText(nativeAd.getAdCallToAction());

        // Downloading and setting the ad icon.
        NativeAd.Image adIcon = nativeAd.getAdIcon();
        NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);

        // Downloading and setting the cover image.
        NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
        int bannerWidth = adCoverImage.getWidth();
        int bannerHeight = adCoverImage.getHeight();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        int screenWidth = metrics.widthPixels;
        int screenHeight = metrics.heightPixels;
        nativeAdMedia.setLayoutParams(new FrameLayout.LayoutParams(
                (int) (screenWidth - metrics.density * 32), (int) (((double) (screenWidth - metrics.density * 32) / (double) bannerWidth) * bannerHeight)
        ));
//        NativeAd.downloadAndDisplayImage(adCoverImage, nativeAdImage);
        nativeAdImage.setVisibility(View.GONE);
        nativeAdMedia.setNativeAd(nativeAd);

        if (adChoiceContainer.getChildCount() < 1) {
            // Add the AdChoices icon
            AdChoicesView adChoicesView = new AdChoicesView(context, nativeAd, true);
            adChoiceContainer.addView(adChoicesView);
        }

//            nativeAdMedia.setClickable(false);

        // Wire up the View with the native ad, the whole nativeAdContainer will be clickable
//            nativeAd.registerViewForInteraction(adView);
//            nativeAd.registerViewForInteraction(nativeAdIcon);
            nativeAd.registerViewForInteraction(nativeCallToAction);

        // Or you can replace the above call with the following function to specify the clickable areas.
//        nativeAd.registerViewForInteraction(nativeCallToAction, Arrays.asList(nativeAdIcon, nativeCallToAction));
    }
}

